package pms.dao.impl;


import pms.dao.*;
import pms.entity.*;
import pms.until.JDBCTools;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RecordDaoImpl implements RecordDao {
    private JDBCTools tools=new JDBCTools();
    @Override
    public boolean addRecord(Record record) {
        int i=tools.update("insert into t_record(record_time,record_user,record_content) value(?,?,?)",record.getRecord_time(),record.getRecord_user(),record.getRecord_content());
        return i>0;
    }

    @Override
    public List<Record> queryAllRecord() {
        List<Record> list=new ArrayList<>();
        ResultSet rs=tools.query("select * from t_record");
        try {
            while (rs.next()){
                Record record=new Record();
                record.setRecord_id(rs.getInt("record_id"));
                record.setRecord_time(rs.getString("record_time"));
                record.setRecord_user(rs.getString("record_user"));
                record.setRecord_content(rs.getString("record_content"));
                list.add(record);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return list;
    }

    @Override
    public boolean deleteAllRecord(List list) {
        int i=tools.update("delete from t_record");
        return i>0;
    }

    @Override
    public List<Medicine> queryAllMedicine() {
        List<Medicine> list=new ArrayList<>();
        ResultSet rs=tools.query("select * from t_medicine");
        try {
            while(rs.next()){
                Medicine medicine1=new Medicine();
                medicine1.setMedicine_id(rs.getInt("medicine_id"));
                medicine1.setMedicine_name(rs.getString("medicine_name"));
                medicine1.setMedicine_price(rs.getDouble("medicine_price"));
                medicine1.setMedicine_date(rs.getDate("medicine_date").toLocalDate());
                medicine1.setMedicine_number(rs.getInt("medicine_number"));
                medicine1.setMedicine_thd(rs.getInt("medicine_thd"));
                list.add(medicine1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return list;
    }

    @Override
    public List<Supplier> queryAllSupplier() {
        List<Supplier> list = new ArrayList<>();
        ResultSet rs = tools.query("select * from t_supplier");
        try {
            while (rs.next()) {
                Supplier supplier = new Supplier();
                supplier.setSupplier_name(rs.getString("supplier_name"));
                supplier.setSupplier_price(rs.getInt("supplier_price"));
                supplier.setSupplier_qct(rs.getInt("supplier_qct"));
                supplier.setSupplier_status(rs.getString("supplier_status"));
                supplier.setSupplier_tel(rs.getString("supplier_tel"));
                supplier.setSupplier_id(rs.getInt("supplier_id"));
                supplier.setSupplier_mainMedicine(rs.getInt("supplier_mainMedicine"));
                list.add(supplier);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            tools.clossAll();
        }
        return list;
    }

    @Override
    public List<Order> queryAllOrder() {
        List<Order> list=new ArrayList<>();
        ResultSet rs=tools.query("select * from t_order");
        try {
            while (rs.next()){
                Order order=new Order();
                order.setOrder_id(rs.getInt("order_id"));
                order.setOrder_amount(rs.getInt("order_amount"));
                order.setOrder_date(rs.getDate("order_date").toLocalDate());
                order.setOrder_status(rs.getInt("order_status"));
                int supplierid=rs.getInt("order_supplierid");
                int medicineid=rs.getInt("order_medicine");
                SupplierDao dao=new SupplierDaoImpl();
                order.setOrder_supplier(dao.querySupplierById(supplierid));
                MedicineDao dao1=new MedicineDaoImpl();
                order.setOrder_medicine(dao1.queryNameById(medicineid));
                list.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return list;
    }

    @Override
    public List<Sale> queryAllSale() {
        List<Sale> list=new ArrayList<>();
        ResultSet rs=tools.query("select * from t_sale");
        Sale sale=null;
        try {
            while (rs.next()){
                sale=new Sale();
                sale.setSale_id(rs.getInt("sale_id"));
                sale.setSale_amount(rs.getDouble("sale_amount"));
                sale.setSale_date(rs.getDate("sale_date").toLocalDate());
                sale.setSale_number(rs.getInt("sale_number"));
                int customerid=rs.getInt("sale_customer");
                int empid=rs.getInt("sale_emp");
                int medicineid=rs.getInt("sale_medicine");
                CustomerDao dao=new CustomerDaoImpl();
                MedicineDao dao1=new MedicineDaoImpl();
                EmpDao dao2=new EmpDaoImpl();
                sale.setSale_customer(dao.queryCustomerNameByID(customerid));
                sale.setSale_medicine(dao1.queryNameById(medicineid));
                sale.setSale_emp(dao2.queryEmpNameById(empid));
                list.add(sale);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return list;
    }

    @Override
    public List<Customer> queryAllCustomer() {
        List<Customer> list=new ArrayList<>();
        ResultSet rs=tools.query("select * from t_customer");
        try {
            while (rs.next()){
                Customer customer=new Customer();
                customer.setCustomer_name(rs.getString("customer_name"));
                customer.setCustomer_id(rs.getInt("customer_id"));
                customer.setCustomer_level(rs.getString("customer_level"));
                customer.setCustomer_tel(rs.getString("customer_tel"));
                list.add(customer);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return list;
    }
}
